package de.jamm.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;

public class Log {
	private static Notification currNote;
	private static FileWriter writer = null;
	
	public static void d(String tag,String value){
		if(GameSettings.isLogCheck()){
			if(writer == null)
				openLog();
			
			try {
				writer.write(new SimpleDateFormat("[HH:mm:ss]").format(new Date()) + tag+": "+value);
				writer.write(System.getProperty("line.separator"));
				writer.flush();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.err.println(e);
			}
		}
		//LogginContent.getTextPane().append(new SimpleDateFormat("[HH:mm:ss]").format(new Date()) + tag+": ");
        LogginContent.getTextPane().append(value+"\n");
        
        LogginContent.repaintAll();
	}
	private static void openLog(){
        File dir = new File("Log");
        if(!dir.exists())
        	dir.mkdirs();
        
        File file = new File("Log/log_"+new SimpleDateFormat("[HH_mm_ss]").format(new Date())+".txt");
        
        try {
			writer = new FileWriter(file ,true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void closeLog(){
		if(writer != null){
			
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			writer = null;
		} 
	}
	public static void n(String title,String text,int lange){
		if(currNote == null){
			currNote = new Notification(title,text,lange);
			currNote.start();
		}
	}
	public static void deleteNote(){
		currNote = null;
	}
	public static void m(String miniMap){
		MiniMap.getInstance().setMiniMap(miniMap);
		MiniMap.getInstance().getMiniMap().repaint();
	}
}
class Notification extends Thread{
	JWindow w;
	String text;
	String title;
	int lange;
	public Notification(String title,String text,int lange) {
		this.lange = lange;
		this.text = text;
		this.title = title;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
			JFrame mainFrame = GuiController.getInstance().getMainFrame();
			w = new JWindow(mainFrame);
			JPanel content = (JPanel) w.getContentPane();
			content.setBorder(new LineBorder(Color.WHITE));
			content.setBackground(Color.BLACK);
			
			w.setSize(new Dimension(250,100));
			w.setLocation(mainFrame.getWidth()/2-w.getWidth()/2, mainFrame.getHeight());
			
			
			//w.setOpacity(0.1f);
			content.setForeground(Color.WHITE);
			
			JLabel txt = new JLabel(text,JLabel.CENTER);
			txt.setForeground(Color.WHITE);
			
			
			JLabel lbl = new JLabel(" >>"+title,JLabel.LEFT);
			lbl.setForeground(Color.WHITE);
			lbl.setFont(new Font(txt.getFont().getName(), Font.BOLD, 13));
			
			content.setLayout(new BorderLayout());
			content.add(txt,BorderLayout.CENTER);
			content.add(lbl,BorderLayout.NORTH);

			w.setBackground(new Color(0,0,0,90));
			content.setBackground(new Color(0,0,0,0));
			w.setVisible(true);
			
			for(int i = 0;i <= 98;i++){
				i+=2;
				w.setLocation(mainFrame.getWidth()/2-w.getWidth()/2, mainFrame.getHeight()-i);
				GuiController.getInstance().repaintGui();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			try {
				Thread.sleep(lange);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			for(int i = 98;i >= 0;i--){
				i-=2;
				w.setLocation(mainFrame.getWidth()/2-w.getWidth()/2, mainFrame.getHeight()-i);
				GuiController.getInstance().repaintGui();
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			w.dispose();
			GuiController.getInstance().repaintGui();
			Log.deleteNote();
	}
}
